home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / UCRASM25.ARJ / STRSTR.ASM < prev    next >
Assembly Source File  |  1991-10-12  |  2KB  |  94 lines

  1. StdGrp        group    stdlib,stddata
  2. stddata        segment    para public 'sldata'
  3. stddata        ends
  4. ;
  5. stdlib        segment    para public 'slcode'
  6.         assume    cs:stdgrp
  7. ;
  8. ;
  9. ; strstr- Returns the position of a substring in another string.
  10. ;
  11. ; inputs:
  12. ;
  13. ;    es:di- address of string to search through.
  14. ;    dx:si- address of substring to search for.
  15. ;
  16. ;
  17. ; returns: 
  18. ;
  19. ;    cx- position of character in string (if present).
  20. ;    carry=0 if character found.
  21. ;    carry=1 if character is not present in string.
  22. ;
  23.         public    sl_strstr
  24. ;
  25. sl_strstr    proc    far
  26.         push    ds
  27.         push    es
  28.         pushf
  29.         push    si
  30.         push    di
  31.         push    ax
  32.         push    bx
  33.         push    dx
  34.         cld
  35.         mov    ax, es
  36.         mov    es, dx
  37.         mov    ds, ax
  38.         xchg    si, di
  39. ;
  40.         mov    bx, di        ;Save ptr to substring.
  41. ;
  42. ; Compute the length of the substring:
  43. ;
  44.         mov    cx, 0ffffh
  45.         mov    al, 0
  46.     repne    scasb
  47.         neg    cx
  48.         dec    cx
  49.         dec    cx
  50.         mov    dx, cx        ;Save length of smaller string.
  51. ;
  52.         mov    ax, si        ;Save ptr to string.
  53. StrLp:        mov    cx, dx
  54.     repe    cmpsb            ;Compare the strings
  55.         jz    StrsAreEql    ;Jump if substring exists.
  56.         inc    ax        ;Bump pointer into string.
  57.         mov    si, ax        ;Restore pointers.
  58.         mov    di, bx
  59.         cmp    byte ptr [si], 0 ;Done yet?
  60.         jne    StrLp
  61. ;
  62. ; Bad news down here, the substring isn't present in the source string.
  63. ;
  64.         xor    cx, cx
  65.         pop    dx
  66.         pop    bx
  67.         pop    ax
  68.         pop    di
  69.         pop    si
  70.         popf
  71.         pop    es
  72.         pop    ds
  73.         stc
  74.         ret
  75. ;
  76. StrsAreEql:
  77.         mov    cx, ax            ;Save ptr to string
  78.         pop    dx
  79.         pop    bx
  80.         pop    ax
  81.         pop    di
  82.         sub    cx, di            ;Compute index to substring.
  83.         pop    si
  84.         popf
  85.         clc
  86.         pop    es
  87.         pop    ds
  88.         ret
  89. sl_strstr    endp
  90. ;
  91. ;
  92. stdlib        ends
  93.         end
  94.